/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Spinner;

import id.duapuluhdua.dakidaki.Preferences;
import id.duapuluhdua.dakidaki.R;
import id.duapuluhdua.dakidaki.common.CommonActivity;

/**
 *
 */
public class ActivityPreferences extends CommonActivity
{
  /** Fields */
  private RadioButton mMeasureSystemUSA;
  private RadioButton mMeasureSystemEurope;

  private Spinner mListLocationUpdatePeriod;
  private Spinner mListLocationAccuracy;

  private String[] mVecUpdatePeriod;
  private String[] mVecAccuracy;

  private CheckBox mLocationEditDecimal;

  private int mOldIntervalIndex = -1;
  private int mNewIntervalIndex = -1;

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_preferences);

    setSubTitle(R.string.title_preferences);

    mMeasureSystemUSA = (RadioButton)findViewById(R.id.rbtnMeasureSystemUSA);
    mMeasureSystemEurope = (RadioButton)findViewById(R.id.rbtnMeasureSystemEurope);

    mListLocationUpdatePeriod = (Spinner)findViewById(R.id.listLocationUpdatePeriod);
    mListLocationAccuracy = (Spinner)findViewById(R.id.listLocationAccuracy);

    mVecUpdatePeriod = mContext.getResources().getStringArray(R.array.location_update_periods);
    mVecAccuracy = mContext.getResources().getStringArray(R.array.location_accuracy);

    setItemsForSpinner(mListLocationUpdatePeriod, mVecUpdatePeriod);
    setItemsForSpinner(mListLocationAccuracy, mVecAccuracy);

    mLocationEditDecimal = (CheckBox)findViewById(R.id.chkLocationEditDecimal);
  }

  @Override
  protected void onStart()
  {
    super.onStart();

    mPreference.load();

    if (mPreference.measureMode == Preferences.MEASURE_MODE_USA) {
      mMeasureSystemUSA.setChecked(true);
    }
    if (mPreference.measureMode == Preferences.MEASURE_MODE_EU) {
      mMeasureSystemEurope.setChecked(true);
    }

    mOldIntervalIndex = mPreference.getIntervalOptionIndex();
    if (mOldIntervalIndex != -1) {
      mListLocationUpdatePeriod.setSelection(mOldIntervalIndex);
    }

    final int iAccuracyIndex = mPreference.getLocationAccuracyIndex();
    if (iAccuracyIndex != -1) {
      mListLocationAccuracy.setSelection(iAccuracyIndex);
    }
    mLocationEditDecimal.setChecked(mPreference.bLocationEditDecimal);
  }

  /** Button click listener */
  public void onDoneClick(View v)
  {
    if (mMeasureSystemUSA.isChecked()) {
      mPreference.measureMode = Preferences.MEASURE_MODE_USA;
    }
    if (mMeasureSystemEurope.isChecked()) {
      mPreference.measureMode = Preferences.MEASURE_MODE_EU;
    }

    mNewIntervalIndex = mListLocationUpdatePeriod.getSelectedItemPosition();
    mPreference.setUpdatePeriodItemIndex(mNewIntervalIndex);

    final int iAccuracyIndex = mListLocationAccuracy.getSelectedItemPosition();
    mPreference.setLocationAccuracyIndex(iAccuracyIndex);

    mPreference.bLocationEditDecimal = mLocationEditDecimal.isChecked();

    mPreference.save();

    restartGpsIfPeriodChanged();

    final Intent it = new Intent(Intent.ACTION_DEFAULT);
    setResult(Activity.RESULT_OK, it);

    finish();
  }

  private void restartGpsIfPeriodChanged()
  {
    if ((mNewIntervalIndex != -1) && (mOldIntervalIndex != -1)) {
      if (mNewIntervalIndex != mOldIntervalIndex) {
        ActivityMain.restartGPS();
      }
    }
  }
}
